import { defineStore } from "pinia";
import {
  listInventoryPage, //盘点单分页查询
} from "@/utils/api/ApprovalManagement/APcheck/APcheck";
import { dictlist } from "@/utils/api/dict/dict"; //字典列表
import { useMemberStore } from "@/stores/modules/member";

const tableHeader = [
  "盘点单号",
  "盘点类型",
  "盘点时间",
  "盘点状态",
  "操作人",
  "审批状态",
  "审批人",
  "审批时间",
  "操作",
];
export const useAPinventoryStore = defineStore(
  "APinventory",
  () => {
    // 定义表头
    const APinventory_tableHeader = tableHeader;
    //盘点单分页列表
    let Inventorylist: any = [];
    let APinventory_list = reactive({ Inventorylist });
    //总条数
    let totalCount: any = ref(0);
    // 盘点状态列表
    let StatusList: any = [];
    let inventoryStatusList: any = reactive({ StatusList });
    // 盘点类型列表
    let TypeList: any = [];
    let inventoryTypeList: any = reactive({ TypeList });

    /******************** 公共方法 ***********************/
    //获取盘点单分页列表
    const getListInventoryPage = async (data: any) => {
      const userStore: any = useMemberStore();
      // 调用接口获取数据
      const res = await listInventoryPage({
        pageNum: data.pageNum || 1,
        pageSize: data.pageSize || 10,
        searchParam: data.searchParam || "",
        StartDate: data.StartDate || "",
        EndDate: data.EndDate || "",
        InventoryStatus: data.InventoryStatus || "",
        InventoryType: data.InventoryType || "",
        ReplyStatuss: ["CheckNot", "CheckFail", "CheckPass"],
        GodownCode: userStore.GodownCode,
        GodownKeeper: userStore.user_id,
      });
      let resdata = res?.data;
      resdata?.list.forEach((item: any) => {
        item.checked = false;
      });
      APinventory_list.Inventorylist = resdata?.list;
      totalCount.value = resdata?.totalCount;
      return resdata;
    };
    //获取盘点状态列表
    const getDictList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "InventoryStatus",
      });
      inventoryStatusList.StatusList = res.data;
      return res.data;
    };
    //获取盘点类型列表
    const getInventoryTypeList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "InventoryType",
      });
      inventoryTypeList.TypeList = res.data;
      return res.data;
    };
    /*********************** 对外暴露 ********************************/
    return {
      APinventory_tableHeader,
      getListInventoryPage,
      APinventory_list,
      getDictList,
      getInventoryTypeList,
      inventoryStatusList,
      inventoryTypeList,
      totalCount,
    };
  },
  /**********************************TODO: 持久化***********************/
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ["APinventory_tableHeader", ""],
    },
  }
);
